#include "friendservice.h"
#include <QDebug>
FriendService::FriendService(QObject *parent) : QObject(parent)
{

}

void FriendService::RecvAddFriendRequest(std::shared_ptr<PDU> recvBag)
{
    char * InfoChar=new char[1000000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof (PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject Body=dc.object();
    delete [] InfoChar;
    InfoChar=nullptr;

    emit EmitAddFriendInform(Body);
}


void FriendService::RecvFriendInformRespond(std::shared_ptr<PDU> recvBag)
{
    QList<QMap<QString,QVariant>>InfoList;

    char * InfoChar=new char[1000000]{'\0'};

    memcpy(InfoChar,(char*)recvBag.get()+sizeof(PDU),recvBag->MsgLength);

    QJsonDocument doc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());

    if(doc.isNull())return;

    if(!doc.isObject())return;

    QJsonObject Info=doc.object();

    QJsonArray InformArray=Info["Inform"].toArray();

    for(int i=0;i<InformArray.size();i++){

        QMap<QString,QVariant>AInform;

        AInform["SenderId"]=InformArray[i].toObject()["SenderId"].toVariant();

        AInform["RequestStatus"]=InformArray[i].toObject()["RequestStatus"].toVariant();

        AInform["CreateTime"]=InformArray[i].toObject()["RequestStatus"].toVariant();

        InfoList.append(AInform);
    }
    emit EmitFriendInformList(InfoList);
}

void FriendService::RecvAddFriendRespond(std::shared_ptr<PDU> recvBag)
{
    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof (PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject body=dc.object();

    emit GlobalNoticeAddFriendStatus(body);
}

void FriendService::RecvFriendListRespond(std::shared_ptr<PDU> recvBag)
{
    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof (PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    delete [] InfoChar;
    InfoChar=nullptr;
    if(dc.isNull())return;
    if(!dc.isObject())return;
    QJsonObject body=dc.object();

    emit EmitFriendList(body);

}
